package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import com.SmsModule.bean.TbSmsSend;
import com.SmsModule.dao.TbSmsSendDAO;
import com.UserModule.bean.TbUserBean;

public class QunfaThree implements Runnable {

	private String typeId;
	private String tempPath;
	private List<String> backPhoneList;
	private TbUserBean tbUser;
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("dddd"+typeId+"=="+tempPath);
		new QunfaThree().sendSms(typeId, tempPath, backPhoneList, tbUser);
	}

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-5-5
	 *下午01:56:07
	 *zzClub
	 *util
	 */
	/**
	 * typeId区分是导入的txt还是excel
	 * tempPath
	 * 黑名单
	 */
	public void sendSms(String typeId,String tempPath,List<String> backPhoneList,TbUserBean tbUser){
		try{
			List<TbSmsSend> saveSmsList = new ArrayList<TbSmsSend>();//要保存的发送短信
			if(typeId.equals("0")){//Excel导入
				Workbook wb;
				InputStream is2 = new FileInputStream(tempPath);
				wb = Workbook.getWorkbook(is2);
				Sheet sheet = wb.getSheet(0);
				int row = sheet.getRows();
				for(int i =1;i<row;i++){
					TbSmsSend smsSend = new TbSmsSend();
					Cell phoneCell= sheet.getCell(0, i);
					String phone = phoneCell.getContents();
					Cell contentCell = sheet.getCell(1, i);
					String content = contentCell.getContents();
					if(phone.equals("")&&content.equals("")){
						break;
					}
					if(!backPhoneList.contains(phone)){
						smsSend.setCreateTime(Constant.decodeDate());
						smsSend.setDestAdd(phone);
						smsSend.setIsSendTime(0);
						smsSend.setSendTime(Constant.decodeDate());
						smsSend.setSmsContent(content);
						smsSend.setSmsSendState(0);//发送状态０:未发送１:成功２:失败
						smsSend.setUserId(tbUser.getUserId());
						smsSend.setSeriseId(String.valueOf(tbUser.getOrgNum()));
						saveSmsList.add(smsSend);
					}
				}
			}else if(typeId.equals("1")){//txt导入
				BufferedReader bs = new BufferedReader(new FileReader(new File(tempPath)));
				String readContent="";
				while((readContent=bs.readLine())!=null){
					TbSmsSend smsSend = new TbSmsSend();
					String rowContent = readContent;
					String phone = rowContent.split(":")[0];
					String content = rowContent.split(":")[1];
					if(!backPhoneList.contains(phone)){
						smsSend.setCreateTime(Constant.decodeDate());
						smsSend.setDestAdd(phone);
						smsSend.setIsSendTime(0);
						smsSend.setSendTime(Constant.decodeDate());
						smsSend.setSmsContent(content);
						smsSend.setSmsSendState(0);//发送状态０:未发送１:成功２:失败
						smsSend.setUserId(tbUser.getUserId());
						smsSend.setSeriseId(String.valueOf(tbUser.getOrgNum()));
						saveSmsList.add(smsSend);
					}
				}
			}
			
				boolean bo = new TbSmsSendDAO().addSmsList(saveSmsList);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public String getTempPath() {
		return tempPath;
	}

	public void setTempPath(String tempPath) {
		this.tempPath = tempPath;
	}

	public List<String> getBackPhoneList() {
		return backPhoneList;
	}

	public void setBackPhoneList(List<String> backPhoneList) {
		this.backPhoneList = backPhoneList;
	}

	public TbUserBean getTbUser() {
		return tbUser;
	}

	public void setTbUser(TbUserBean tbUser) {
		this.tbUser = tbUser;
	}
}
